<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Product;
class ProductController extends Controller
{
    
    //返回产品管理
    public  function index(Request $request)
    {
       // $users_id = $request->session()->get('user_id');
       
       // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
           
        $product = DB::table('product')
                    ->select('product.*','type.name')
                    ->where('product.is_del',0)
                   // ->where('product.city_id',$city_id->id)
                    
                    ->leftJoin('type','product.type_id','=','type.id')
                    ->orderBy('product.created_at','DESC')
                    ->get();
         
       
        return view('admin.product.index',['list'=>$product]);
        
    }
    
    
    public  function true_top()
    {
         $id = request('id');
        
         $info = Product::find($id);

         $info->is_top  = 1;

        $status = $info->save();

         if($status){
                   return back()->with('err','推荐成功');
              }else{
                   return back()->with('err','推荐失败');
        }
    }
    
    
    public function  false_top()
    {
         $id = request('id');
        
         $info = Product::find($id);

         $info->is_top  = 0;

        $status = $info->save();

         if($status){
                   return back()->with('err','推荐成功');
              }else{
                   return back()->with('err','推荐失败');
        }
    }

    //添加产品管理
    public function create(Request $request)
    {
        
           // $users_id = $request->session()->get('user_id');
       
           // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
        
            $list = \DB::select("select * from type  order by concat(path,id) asc");
        
            //处理信息
            foreach($list as &$v){
                $m = substr_count($v->path,","); //获取path中的逗号
                //生成缩进
                $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
            }
            
           return view('admin.product.create',['list'=>$list]);
    }
    
    public function store(Request $request)
    {
            
           // $users_id = $request->session()->get('user_id');
           // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
            //获取指定的部分数据
        
        
            $data['pro_name'] = $request->input('product_name');
            $data['title'] = $request->input('title');
            $data['content'] = trim($request->input('content'));
            $data['type_id'] = $request->input('pid');
            $data['y_price'] = $request->input('y_price');
            $data['price'] = $request->input('price');
            $data['xuzhi'] = $request->input('xuzhi');
            
           // $data['city_id'] = $city_id->id;
             
            if(!$request->hasFile('pic')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
             
            $img = $request->file('pic');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['img'] = $img->store(date('Ymd'));
            
            
            if(!$request->hasFile('p_img')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('p_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['p_img'] = $img->store(date('Ymd'));
            
            
            
            
            $data['created_at'] = date("Y-m-d H:i:s");
            
             $id = \DB::table("product")->insertGetId($data);
        
           if($id>0){
                 return redirect('product');
             }else{
                return back()->with("err","添加失败!");
             } 
    }
    
    
     public function destroy($id)
     {
         
      $info =  \DB::table('product')
            ->where('id',$id)
            ->update(['is_del' => 1]);
    
      if($info){
           return redirect("product")->with("err","删除成功！");
      }else{
           return redirect("product")->with("err","删除失败！");
      }
       
    }
    
    //加载修改表单
    public function edit($id,Request $request)
    {
        
        
        //$users_id = $request->session()->get('user_id');
        //$city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
        
        
        $info = \DB::table('product')->where('id',$id)->first();
        
        $list = \DB::select("select type.* from type  order by concat(type.path,type.id) asc");
        //处理信息
            foreach($list as &$v){
                $m = substr_count($v->path,","); //获取path中的逗号
                //生成缩进
                $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
            }
      
        return view('admin.product.edit',['info'=>$info,'list'=>$list]);
           
    }
    
    
    public function update($id,Request $request)
    {
            
        
            $data['pro_name'] = $request->input('product_name');
            $data['title'] = $request->input('title');
            $data['content'] = trim($request->input('content'));
            $data['type_id'] = $request->input('pid');
            $data['y_price'] = $request->input('y_price');
            $data['price'] = $request->input('price');
             
           if($request->hasFile('pic')){
                $img = $request->file('pic');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['img'] = $img->store(date('Ymd'));
             }else{
                   $info = \DB::table("product")->where("id",$id)->first();
                   $data['img']  = $info->img;
             }
                 
           if($request->hasFile('p_img')){
                $img = $request->file('p_img');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['p_img'] = $img->store(date('Ymd'));
             }else{
                   $info = \DB::table("product")->where("id",$id)->first();
                   $data['p_img']  = $info->p_img;
               
             }
                     
            $data['updated_at'] = date("Y-m-d H:i:s");
            
            $id = \DB::table("product")->where("id",$id)->update($data);
        
           if($id>0){
                 return redirect('product');
             }else{
                return back()->with("err","修改失败!");
             } 
        
        
    }
    
}
